package com.diy.sigmund.diycommon.util;

import com.diy.sigmund.diycommon.entity.base.exception.DiyRuntimeException;
import com.diy.sigmund.diycommon.entity.enumerate.ResponseEnum;
import com.diy.sigmund.diycommon.util.time.DateUtil;
import java.util.Date;
import java.util.Objects;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;

/**
 * @author ylm-sigmund
 * @since 2021/5/24 22:03
 */
public class VerifyParamUtil {

    public static final String PHONE_PATTERN = "^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$";
    public static final String EMAIL_PATTERN = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
    /**
     * 18位身份证号
     */
    public static final String IDCARD_PATTERN = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{4}$";
    /**
     * 分隔符
     */
    private static final String DELIMITER = ",";


    public static boolean isNotNull(Object fieldValueObj, Object verifyValueObj) {
        return Objects.nonNull(fieldValueObj);
    }

    /**
     * 检查字符串是否不为空 ("") 且不为 null
     *
     * @param fieldValueObj  fieldValueObj
     * @param verifyValueObj verifyValueObj
     * @return boolean
     */
    public static boolean isNotEmpty(Object fieldValueObj, Object verifyValueObj) {
        return StringUtils.isNotEmpty(fieldValueObj.toString());
    }

    /**
     * 检查字符串是否不为空 ("")、不为空且不只包含空格
     *
     * @param fieldValueObj  fieldValueObj
     * @param verifyValueObj verifyValueObj
     * @return boolean
     */
    public static boolean isNotBlank(Object fieldValueObj, Object verifyValueObj) {
        return StringUtils.isNotBlank(fieldValueObj.toString());
    }

    /**
     * 字符串校验正则
     * 注意：仅适用于参数校验注解，因为这2个入参的位置与常用的不一致
     *
     * @param regex          The expression to be compiled
     * @param verifyValueObj The character sequence to be matched
     * @return whether or not the regular expression matches on the input
     */
    public static boolean isNotMatches(Object verifyValueObj, Object regex) {
        return Pattern.matches(regex.toString(), verifyValueObj.toString());
    }


    public static boolean assertTrue(Object fieldValueObj, Object obj) {
        return fieldValueObj.equals(true);
    }

    public static boolean assertFalse(Object fieldValueObj, Object obj) {
        return fieldValueObj.equals(false);
    }

    public static boolean min(Object fieldValueObj, Object verifyValueObj) {
        int fieldValue;
        int verifyValue;
        try {
            fieldValue = Integer.parseInt(fieldValueObj.toString());
            verifyValue = Integer.parseInt(verifyValueObj.toString());
        } catch (NumberFormatException e) {
            throw new DiyRuntimeException(ResponseEnum.BODY_NOT_MATCH);
        }
        return fieldValue >= verifyValue;
    }

    public static boolean max(Object fieldValueObj, Object verifyValueObj) {
        int fieldValue;
        int verifyValue;
        try {
            fieldValue = Integer.parseInt(fieldValueObj.toString());
            verifyValue = Integer.parseInt(verifyValueObj.toString());
        } catch (NumberFormatException e) {
            throw new DiyRuntimeException(ResponseEnum.BODY_NOT_MATCH);
        }
        return fieldValue <= verifyValue;
    }

    public static Boolean betweenInteger(Object fieldValueObj, Object verifyValueObj) {
        int fieldValue;
        int minValue;
        int maxValue;
        try {
            final String[] split = getSplitStr(verifyValueObj);
            fieldValue = Integer.parseInt(fieldValueObj.toString());
            minValue = Integer.parseInt(split[0]);
            maxValue = Integer.parseInt(split[1]);
        } catch (Exception e) {
            throw new DiyRuntimeException(ResponseEnum.BODY_NOT_MATCH);
        }
        return fieldValue >= minValue && fieldValue <= maxValue;
    }

    public static String[] getSplitStr(Object verifyValueObj) {
        return verifyValueObj.toString().split(DELIMITER);
    }

    public static Boolean past(Object fieldValueObj, Object verifyValueObj) {
        Date fieldValue;
        Date verifyValue;
        try {
            fieldValue = (Date) fieldValueObj;
            verifyValue = DateUtil
                    .parseStrToDate(verifyValueObj.toString(), DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        } catch (Exception e) {
            throw new DiyRuntimeException(ResponseEnum.BODY_NOT_MATCH);
        }
        return fieldValue.before(verifyValue);
    }

    public static Boolean future(Object fieldValueObj, Object verifyValueObj) {
        Date fieldValue;
        Date verifyValue;
        try {
            fieldValue = (Date) fieldValueObj;
            verifyValue = DateUtil
                    .parseStrToDate(verifyValueObj.toString(), DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        } catch (Exception e) {
            throw new DiyRuntimeException(ResponseEnum.BODY_NOT_MATCH);
        }
        return fieldValue.after(verifyValue);
    }

    public static Boolean betweenDate(Object fieldValueObj, Object verifyValueObj) {
        Date fieldValue;
        Date minValue;
        Date maxValue;
        try {
            final String[] split = getSplitStr(verifyValueObj);
            fieldValue = (Date) fieldValueObj;
            minValue = DateUtil.parseStrToDate(split[0], DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
            maxValue = DateUtil.parseStrToDate(split[1], DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        } catch (Exception e) {
            throw new DiyRuntimeException(ResponseEnum.BODY_NOT_MATCH);
        }
        return fieldValue.after(minValue) && fieldValue.before(maxValue);
    }
}